﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;

namespace RutTrichThongTin
{
    public class LayTyGiaTuServer
    {
        HttpClient client = new HttpClient();
        HttpResponseMessage response;
        string apiLink = "api/tygiangoaite";

        public LayTyGiaTuServer()
        {
            client.BaseAddress = new Uri("http://localhost:1303/");

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
        }

        public List<TyGiaNgoaiTe> LayDanhSachTuServer()
        {
            response = client.GetAsync(apiLink).Result;
            if (response.IsSuccessStatusCode)
            {
                var ds = response.Content.ReadAsAsync<IEnumerable<TyGiaNgoaiTe>>().Result;
                return (List<TyGiaNgoaiTe>)ds;
            }
            return null;
        }

        public Dictionary<string, Double> LayDanhSachThongTinTyGia()
        {
            List<TyGiaNgoaiTe> lst = LayDanhSachTuServer();
            return CoverterToDictionary(lst);
        }

        public Dictionary<string, Double> CoverterToDictionary(List<TyGiaNgoaiTe> lst)
        {
            Dictionary<string, Double> dsTyGia = new Dictionary<string, double>();
            int n = lst.Count;
            for (int i = 0; i < n; i++)
            {
                dsTyGia.Add(lst[i].LoaiNgoaiTe, lst[i].TyGia);
            }
            return dsTyGia;
        }
    }
}